package cn.itsource.pethome.system.dictionary.contrllor;

import cn.itsource.pethome.system.dictionary.domain.Systemdictionarytype;
import cn.itsource.pethome.system.dictionary.query.SystemdictionarytypeQuery;
import cn.itsource.pethome.system.dictionary.service.ISystemdictionarytypeService;

import cn.itsource.pethome.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/yyds")
@Api(value = "数据字典的API",description="数据字典的CRUD功能")
public class SystemdictionarytypeContrllor {
    @Autowired
    private ISystemdictionarytypeService systemdictionarytypeService;

    @GetMapping("/list")
    @ApiOperation(value = "查询数据字典所有数据" )
    public List<Systemdictionarytype> findAll(){
        return systemdictionarytypeService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据Id查询数据字典所有数据" )
    public Systemdictionarytype findById(@PathVariable  Long id){
        return systemdictionarytypeService.findnoe(id);
    }

    @PostMapping("pagelist")
    @ApiOperation(value = "分页查询数据字典所有数据" )
    public AjaxResult pageData(@RequestBody SystemdictionarytypeQuery query){
        try {
            List<Systemdictionarytype> employees = systemdictionarytypeService.queryData(query);//获取到所有数据
            Long count = systemdictionarytypeService.queryCount(query);//获取到所有数据
            return AjaxResult.success().setSuccess(true).setData(employees).setTotal(count);//将获取的数据和总条数返回给前端
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success().setSuccess(false).setMsg("操作失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除数据字典数据")
    public AjaxResult delete(@PathVariable Long id){
        try {
            systemdictionarytypeService.delete(id);
            return  AjaxResult.success().setSuccess(true).setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success().setSuccess(false).setMsg(e.getMessage());
        }
    }

    @PutMapping
    @ApiOperation(value = "根据id修改数据字典数据，没有id就添加数据")
    public AjaxResult save(@RequestBody Systemdictionarytype employee){
        try {
            if (employee.getId() != null) {
                systemdictionarytypeService.update(employee);
            }else {
                systemdictionarytypeService.add(employee);
            }
            return AjaxResult.success().setSuccess(true).setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success().setSuccess(false).setMsg("操作失败");
    }

    @PatchMapping("/del")
    @ApiOperation(value = "批量删除数据字典数据")
    public AjaxResult bacthDelete(@RequestBody List<Long> ids){
        System.out.println(ids);
        try {
            systemdictionarytypeService.batchDel(ids);
            return AjaxResult.success().setSuccess(true).setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success().setSuccess(false).setMsg("操作失败");
    }
    
}
